jQuery(document).ready(function($) {


    $(".user_btn").on('click', function(event) {


        var oponent_id = $(this).attr('data-id');
        var current_game = $(this).attr('data-current-game');



        $.ajax({
                url: "get_answer.php",
                type: "POST",
                data: { 
                id: oponent_id,
                current_game: current_game
            } 
        }).done(function( data ) {
            

            var spin = JSON.parse(data);


            if (spin.length > 1) {
                //La pregunta es Corona o Duelo
                
                
                for (var n = 0; n < 6; ++ n){
                    var p = n + 1;
                    var category_crown;
                    if ($(spin[0].questions[n]).length) {
                        category_crown = spin[0].questions[n].question.category;
                        var correct_answer = spin[0].questions[n].question.correct_answer + 1;                       
                        $(".result_crown .respuesta"+ p ).html('<span class="resp">' + spin[0].questions[n].question.answers[spin[0].questions[n].question.correct_answer] + '</span> <small class="cat_small">'+category_crown+'</small> <span class="badge pull-right">'+ correct_answer +'</span>').removeClass('hidden').addClass(category_crown.toLowerCase());    
                    }else{
                        $(".result_crown .respuesta"+ p ).addClass('hidden');
                    };
                    
                }
                $(".crown_duel").removeClass('hidden');


                i = 0;
                var category_duel;
                $(spin[1].questions).each(function(index, el) {

                    i = index + 1;
                    category_duel = el.question.category;
                    var correct_answer = el.question.correct_answer + 1
                    $(".result_duel .respuesta"+ i ).html('<span class="resp">' + el.question.answers[el.question.correct_answer] + '</span> <small class="cat_small">'+el.question.category+'</small> <span class="badge pull-right">'+ correct_answer +'</span>').addClass(category_duel.toLowerCase());
              
                });


                $(".result").addClass('hidden');

            }else{

                //Pregunta normal
                var correct_answer = spin[0].questions[0].question.correct_answer + 1
                $(".pregunta").html(spin[0].questions[0].question.text + ' <small>'+spin[0].questions[0].question.category+'</small>');
                $(".respuesta").html(spin[0].questions[0].question.answers[spin[0].questions[0].question.correct_answer] + ' <span class="badge pull-right">'+ correct_answer +'</span>');

                $(".result").removeClass('hidden');
                $(".crown_duel").addClass('hidden');
            };




        });//done




        event.preventDefault();
        /* Act on the event */
    });




    $('#crown_tab a').click(function (e) {
      e.preventDefault()
      $(this).tab('show')
    })




});